//
//  AllStoryViewController.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/11.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "AllStoryViewController.h"
#import "RmdCollectionCell.h"
#import "StoryModel.h"
#import "StoryDetailViewController.h"
#import <MJRefresh.h>


@interface AllStoryViewController ()

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)NSInteger index;

@end

@implementation AllStoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    self.title = @"精选故事";
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.dataArray = [NSMutableArray array];
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    self.layout.itemSize = CGSizeMake(0.46*Screen_W, 0.37*Screen_H);
    self.layout.minimumLineSpacing = 5;
    self.layout.minimumInteritemSpacing = 5;
    self.layout.sectionInset = UIEdgeInsetsMake(10,10, 10, 10);
    self.collectionView = [[UICollectionView alloc]initWithFrame:(CGRectMake(0, 0,Screen_W, Screen_H))collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.95 alpha:1];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[RmdCollectionCell class] forCellWithReuseIdentifier:@"cell"];
 
    
    [self makeDataSource];
    //上拉加载
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefreshAction)];
    self.index = 0;
    
    
    
}

-(void)makeDataSource{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=0" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *tempDictionary = responseObject;
        
        
        NSDictionary *tempDict = tempDictionary[@"data"];
        
        NSArray *tempArray = tempDict[@"hot_spot_list"];
        
        for (NSDictionary *dic in tempArray) {
            
            
            StoryModel *m  = [[StoryModel alloc]init];
            
            
            [m setValuesForKeysWithDictionary:dic];
            
            [self.dataArray addObject:m];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.collectionView reloadData];
        });
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"错误信息:%@",error);
        
    }];
    
    
    
    
    
    
    
}


//上拉加载
-(void)upRefreshAction{
    
    self.index++;
    
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld",self.index*12];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *tempDictionary = responseObject;
        
        
        NSDictionary *tempDict = tempDictionary[@"data"];
        
        NSArray *tempArray = tempDict[@"hot_spot_list"];
        
        for (NSDictionary *dic in tempArray) {
            
            
            StoryModel *m  = [[StoryModel alloc]init];
            
            
            [m setValuesForKeysWithDictionary:dic];
            
            [self.dataArray addObject:m];
            
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            [self.collectionView.mj_footer endRefreshing];
            [self.collectionView reloadData];
        });
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"错误信息:%@",error);
        
    }];
    
    
    
    
    
}


//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}






//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    RmdCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    StoryModel *m = self.dataArray[indexPath.row];
    
    [cell.coverImageV sd_setImageWithURL:[NSURL URLWithString:m.index_cover] placeholderImage:nil];
    
    cell.titleLabel.text = m.index_title;
    
    [cell.userImageV sd_setImageWithURL:[NSURL URLWithString:m.user[@"avatar_s"]] placeholderImage:nil];
    
    cell.userNameLael.text = m.user[@"name"];
    
    
    
    return cell;
}






#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    StoryDetailViewController *sdvc = [StoryDetailViewController new];
    StoryModel *m = self.dataArray[indexPath.row];
    
    
    sdvc.spot_id = m.spot_id;
    
    
    
    [self.navigationController pushViewController:sdvc animated:YES];
    
    
    
}







//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


@end
